package ru.dadle8.serializers;

import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Serializer;
import ru.dadle8.model.Customer;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

public class CustomerSerializer implements Serializer<Customer> {

    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {
        Serializer.super.configure(configs, isKey);
    }

    @Override
    public byte[] serialize(String s, Customer customer) {
        try {
            if (customer == null) {
                return null;
            } else {
                try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
                     ObjectOutputStream oos = new ObjectOutputStream(bos)) {
                    oos.writeObject(customer);
                    return bos.toByteArray();
                }
            }
        } catch (Exception e) {
            throw new SerializationException("Couldn't serializing Customer to byte[] " + e);
        }
    }

    @Override
    public void close() {
        Serializer.super.close();
    }
}
